{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5.2\n"
     ]
    }
   ],
   "source": [
    "import paddle\n",
    "import paddle.nn.functional as F\n",
    "from paddle.nn import Linear\n",
    "import numpy as np\n",
    "import os\n",
    "import json\n",
    "import random\n",
    "print(paddle.__version__)\n",
    "from paddle.nn import Conv2D,MaxPool2D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(mode='train'):\n",
    "    with open('new_mnist.json')as f:\n",
    "        data=json.load(f)\n",
    "    train_set,val_set,eval_set=data\n",
    "    if mode=='train':\n",
    "        imgs,labels=train_set[0],train_set[1]\n",
    "    elif mode=='valid':\n",
    "        imgs,labels=val_set[0],val_set[1]\n",
    "    elif mode=='eval':\n",
    "        imgs,labels=eval_set[0],eval_set[1]\n",
    "    else:\n",
    "        raise Exception(\"mode can only be one of['train','valid','eval']\")\n",
    "    print(\"训练数据集数量:\",len(imgs))\n",
    "    imgs_length=len(imgs)\n",
    "    index_list=list(range(imgs_length))\n",
    "    BATCHSIZE=100\n",
    "    def data_generator():\n",
    "        if mode=='train':\n",
    "            random.shuffle(index_list)\n",
    "        imgs_list=[]\n",
    "        labels_list=[]\n",
    "        for i in index_list:\n",
    "            img=np.array(imgs[i]).astype('float32')\n",
    "            label=np.reshape(labels[i],[1]).astype('int64')\n",
    "            imgs_list.append(img)\n",
    "            labels_list.append(label)\n",
    "            if len(img_list)==BATCHSIZE:\n",
    "                yield np.array(imgs_list),np.array(label_list)\n",
    "                imgs_list=[]\n",
    "                labels_list=[]\n",
    "        if len(imgs_list)>0:\n",
    "            yield np.array(imgs_list),np.array(labels_list)\n",
    "    return data_generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(mode='train'):\n",
    "    with open('new_mnist.json')as f:\n",
    "        data=json.load(f)\n",
    "    train_set,val_set,eval_set=data\n",
    "    if mode=='train':\n",
    "        imgs,labels=train_set[0],train_set[1]\n",
    "    elif mode=='valid':\n",
    "        imgs,labels=val_set[0],val_set[1]\n",
    "    elif mode=='eval':\n",
    "        imgs,labels=eval_set[0],eval_set[1]\n",
    "    else:\n",
    "        raise Exception(\"mode can only be one of['train','valid','eval']\")\n",
    "    print(\"训练数据集数量:\",len(imgs))\n",
    "    imgs_length=len(imgs)\n",
    "    index_list=list(range(imgs_length))\n",
    "    BATCHSIZE=100\n",
    "    def data_generator():\n",
    "        if mode=='train':\n",
    "            random.shuffle(index_list)\n",
    "        imgs_list=[]\n",
    "        labels_list=[]\n",
    "        for i in index_list:\n",
    "            img=np.reshape(imgs[i],[1,28,28]).astype('float32')\n",
    "            label=np.reshape(labels[i],[1]).astype('int64')\n",
    "            imgs_list.append(img)\n",
    "            labels_list.append(label)\n",
    "            if len(imgs_list)==BATCHSIZE:\n",
    "                yield np.array(imgs_list),np.array(labels_list)\n",
    "                imgs_list=[]\n",
    "                labels_list=[]\n",
    "        if len(imgs_list)>0:\n",
    "            yield np.array(imgs_list),np.array(labels_list)\n",
    "    return data_generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "class LeNetModel(paddle.nn.Layer):\n",
    "    def __init__(self):\n",
    "        super(LeNetModel,self).__init__()\n",
    "        self.conv1=paddle.nn.Conv2D(in_channels=1,out_channels=6,kernel_size=5,stride=1)\n",
    "        self.pool1=paddle.nn.MaxPool2D(kernel_size=2,stride=2)\n",
    "        self.conv2=paddle.nn.Conv2D(in_channels=6,out_channels=16,kernel_size=5,stride=1)\n",
    "        self.pool2=paddle.nn.MaxPool2D(kernel_size=2,stride=2) \n",
    "        self.fc1=paddle.nn.Linear(256,120)\n",
    "        self.fc2=paddle.nn.Linear(120,84)\n",
    "        self.fc3=paddle.nn.Linear(84,10)\n",
    "    def forward(self,x):\n",
    "        x=self.conv1(x)\n",
    "        x=F.relu(x)\n",
    "        x=self.pool1(x)\n",
    "        x=self.conv2(x)\n",
    "        x=F.relu(x)\n",
    "        x=self.pool2(x)\n",
    "        x=paddle.flatten(x,start_axis=1,stop_axis=-1)\n",
    "        x=self.fc1(x)\n",
    "        x=F.relu(x)\n",
    "        x=self.fc2(x)\n",
    "        x=F.relu(x)\n",
    "        x=self.fc3(x)\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train(model):\n",
    "    model.train()\n",
    "    opt=paddle.optimizer.SGD(learning_rate=0.01,parameters=model.parameters())\n",
    "    EPOCH_NUM=5\n",
    "    for epoch_id in range(EPOCH_NUM):\n",
    "        for batch_id,data in enumerate(train_loader()):\n",
    "            images,labels=data\n",
    "            images=paddle.to_tensor(images)\n",
    "            labels=paddle.to_tensor(labels)\n",
    "            predicts=model(images)\n",
    "            loss=F.softmax_with_cross_entropy(predicts,labels)\n",
    "            avg_loss=paddle.mean(loss)\n",
    "            if batch_id %200 ==0:\n",
    "                print(\"epoch:{},batch:{},loss is:{}\".format(epoch_id,batch_id,avg_loss.numpy()))\n",
    "            avg_loss.backward()\n",
    "            opt.step()\n",
    "            opt.clear_grad()\n",
    "        model.eval()\n",
    "        accuracies=[]\n",
    "        losses=[]\n",
    "        for batch_id,data in enumerate(valid_loader()):\n",
    "            images,labels=data\n",
    "            images=paddle.to_tensor(images)\n",
    "            labels=paddle.to_tensor(labels)\n",
    "            logits=model(images)\n",
    "            pred=F.softmax(logits)\n",
    "            loss=F.softmax_with_cross_entropy(logits,labels)\n",
    "            acc=paddle.metric.accuracy(pred,labels)\n",
    "            accuracies.append(acc.numpy())\n",
    "            losses.append(loss.numpy())\n",
    "            print(\"[validation]accuracy/loss:{}/{}\".format(np.mean(accuracies),np.mean(losses)))\n",
    "            model.train()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练数据集数量: 50000\n",
      "训练数据集数量: 10000\n",
      "epoch:0,batch:0,loss is:[3.240928]\n",
      "epoch:0,batch:200,loss is:[0.39743435]\n",
      "epoch:0,batch:400,loss is:[0.308095]\n",
      "[validation]accuracy/loss:0.9200000166893005/0.2792096436023712\n",
      "[validation]accuracy/loss:0.9300000071525574/0.24041135609149933\n",
      "[validation]accuracy/loss:0.9233333468437195/0.28184041380882263\n",
      "[validation]accuracy/loss:0.8999999761581421/0.32696783542633057\n",
      "[validation]accuracy/loss:0.9019999504089355/0.3309873044490814\n",
      "[validation]accuracy/loss:0.9049999713897705/0.3361389935016632\n",
      "[validation]accuracy/loss:0.8999999761581421/0.3382401764392853\n",
      "[validation]accuracy/loss:0.8987500071525574/0.33413955569267273\n",
      "[validation]accuracy/loss:0.897777795791626/0.33201050758361816\n",
      "[validation]accuracy/loss:0.9039999842643738/0.3144223690032959\n",
      "[validation]accuracy/loss:0.9063636660575867/0.3035666048526764\n",
      "[validation]accuracy/loss:0.9066666960716248/0.3011234998703003\n",
      "[validation]accuracy/loss:0.9061537981033325/0.2998690605163574\n",
      "[validation]accuracy/loss:0.9092856645584106/0.2899320423603058\n",
      "[validation]accuracy/loss:0.9106666445732117/0.28503847122192383\n",
      "[validation]accuracy/loss:0.9124999642372131/0.2776602506637573\n",
      "[validation]accuracy/loss:0.9141175746917725/0.27304545044898987\n",
      "[validation]accuracy/loss:0.9138888120651245/0.27475401759147644\n",
      "[validation]accuracy/loss:0.9142104983329773/0.2699006497859955\n",
      "[validation]accuracy/loss:0.9120000004768372/0.27827900648117065\n",
      "[validation]accuracy/loss:0.9095238447189331/0.2866511940956116\n",
      "[validation]accuracy/loss:0.9077273011207581/0.2972148358821869\n",
      "[validation]accuracy/loss:0.9078261256217957/0.3005092740058899\n",
      "[validation]accuracy/loss:0.9079167246818542/0.30413007736206055\n",
      "[validation]accuracy/loss:0.9096000790596008/0.29715126752853394\n",
      "[validation]accuracy/loss:0.9123077392578125/0.28942710161209106\n",
      "[validation]accuracy/loss:0.9137037992477417/0.28729447722435\n",
      "[validation]accuracy/loss:0.9125000238418579/0.29127830266952515\n",
      "[validation]accuracy/loss:0.9110345244407654/0.2976422607898712\n",
      "[validation]accuracy/loss:0.9080000519752502/0.3087916374206543\n",
      "[validation]accuracy/loss:0.907096803188324/0.31001824140548706\n",
      "[validation]accuracy/loss:0.90625/0.30954813957214355\n",
      "[validation]accuracy/loss:0.9069697260856628/0.3074774444103241\n",
      "[validation]accuracy/loss:0.9079412221908569/0.304222047328949\n",
      "[validation]accuracy/loss:0.9088571667671204/0.3014781177043915\n",
      "[validation]accuracy/loss:0.9077777862548828/0.3058382570743561\n",
      "[validation]accuracy/loss:0.9078378677368164/0.30654168128967285\n",
      "[validation]accuracy/loss:0.9086841940879822/0.3038252294063568\n",
      "[validation]accuracy/loss:0.9084615707397461/0.30330416560173035\n",
      "[validation]accuracy/loss:0.9087499380111694/0.30265289545059204\n",
      "[validation]accuracy/loss:0.9063414335250854/0.3058895170688629\n",
      "[validation]accuracy/loss:0.9071428179740906/0.30357521772384644\n",
      "[validation]accuracy/loss:0.908604621887207/0.2998785376548767\n",
      "[validation]accuracy/loss:0.9093181490898132/0.2973627746105194\n",
      "[validation]accuracy/loss:0.9104443788528442/0.2940347492694855\n",
      "[validation]accuracy/loss:0.9099999070167542/0.2953368127346039\n",
      "[validation]accuracy/loss:0.910851001739502/0.29255691170692444\n",
      "[validation]accuracy/loss:0.9112499356269836/0.28999653458595276\n",
      "[validation]accuracy/loss:0.910408079624176/0.29190200567245483\n",
      "[validation]accuracy/loss:0.9091998934745789/0.29589080810546875\n",
      "[validation]accuracy/loss:0.9098038077354431/0.2947545051574707\n",
      "[validation]accuracy/loss:0.9101921916007996/0.2937878966331482\n",
      "[validation]accuracy/loss:0.911320686340332/0.2912754416465759\n",
      "[validation]accuracy/loss:0.9112961888313293/0.29092705249786377\n",
      "[validation]accuracy/loss:0.9118180871009827/0.28957709670066833\n",
      "[validation]accuracy/loss:0.9124999642372131/0.2880309522151947\n",
      "[validation]accuracy/loss:0.9128069877624512/0.28634703159332275\n",
      "[validation]accuracy/loss:0.9125862121582031/0.28832048177719116\n",
      "[validation]accuracy/loss:0.9125424027442932/0.2875186800956726\n",
      "[validation]accuracy/loss:0.9131667017936707/0.2856179177761078\n",
      "[validation]accuracy/loss:0.9136065244674683/0.28398680686950684\n",
      "[validation]accuracy/loss:0.9141935706138611/0.2818763256072998\n",
      "[validation]accuracy/loss:0.914126992225647/0.28250622749328613\n",
      "[validation]accuracy/loss:0.9139062166213989/0.2816166579723358\n",
      "[validation]accuracy/loss:0.9139999151229858/0.28181377053260803\n",
      "[validation]accuracy/loss:0.9146968722343445/0.2800152003765106\n",
      "[validation]accuracy/loss:0.9146267771720886/0.2797732353210449\n",
      "[validation]accuracy/loss:0.91544109582901/0.27718523144721985\n",
      "[validation]accuracy/loss:0.9153622388839722/0.2770279347896576\n",
      "[validation]accuracy/loss:0.9157142639160156/0.27605685591697693\n",
      "[validation]accuracy/loss:0.9154929518699646/0.2762027978897095\n",
      "[validation]accuracy/loss:0.9162499904632568/0.27414846420288086\n",
      "[validation]accuracy/loss:0.9160274267196655/0.27395421266555786\n",
      "[validation]accuracy/loss:0.9158108830451965/0.2743159532546997\n",
      "[validation]accuracy/loss:0.9158666729927063/0.27386045455932617\n",
      "[validation]accuracy/loss:0.9153947234153748/0.2757551670074463\n",
      "[validation]accuracy/loss:0.9154545068740845/0.2764900326728821\n",
      "[validation]accuracy/loss:0.9149999618530273/0.2772639989852905\n",
      "[validation]accuracy/loss:0.914810061454773/0.2766338288784027\n",
      "[validation]accuracy/loss:0.9148750305175781/0.275941401720047\n",
      "[validation]accuracy/loss:0.9154320955276489/0.2753409445285797\n",
      "[validation]accuracy/loss:0.9162195920944214/0.27343130111694336\n",
      "[validation]accuracy/loss:0.9171084761619568/0.2710559368133545\n",
      "[validation]accuracy/loss:0.917738139629364/0.26944515109062195\n",
      "[validation]accuracy/loss:0.9182353019714355/0.2678665220737457\n",
      "[validation]accuracy/loss:0.9184884428977966/0.2669287323951721\n",
      "[validation]accuracy/loss:0.9189655780792236/0.26569056510925293\n",
      "[validation]accuracy/loss:0.9192045331001282/0.2644972503185272\n",
      "[validation]accuracy/loss:0.9195505380630493/0.2636469602584839\n",
      "[validation]accuracy/loss:0.9203332662582397/0.2613505721092224\n",
      "[validation]accuracy/loss:0.9210988283157349/0.2592078447341919\n",
      "[validation]accuracy/loss:0.9216303825378418/0.2574821412563324\n",
      "[validation]accuracy/loss:0.9220429062843323/0.25633466243743896\n",
      "[validation]accuracy/loss:0.9217020273208618/0.25734901428222656\n",
      "[validation]accuracy/loss:0.9217894077301025/0.25730592012405396\n",
      "[validation]accuracy/loss:0.9226041436195374/0.2551852762699127\n",
      "[validation]accuracy/loss:0.9229896664619446/0.2541556656360626\n",
      "[validation]accuracy/loss:0.9223469495773315/0.25796279311180115\n",
      "[validation]accuracy/loss:0.9229293465614319/0.25603821873664856\n",
      "[validation]accuracy/loss:0.9229000210762024/0.25713691115379333\n",
      "epoch:1,batch:0,loss is:[0.26916128]\n",
      "epoch:1,batch:200,loss is:[0.20444575]\n",
      "epoch:1,batch:400,loss is:[0.3561754]\n",
      "[validation]accuracy/loss:0.9800000190734863/0.14792123436927795\n",
      "[validation]accuracy/loss:0.9750000238418579/0.14699406921863556\n",
      "[validation]accuracy/loss:0.9600000381469727/0.189225435256958\n",
      "[validation]accuracy/loss:0.9424999952316284/0.22407934069633484\n",
      "[validation]accuracy/loss:0.9399999380111694/0.22929106652736664\n",
      "[validation]accuracy/loss:0.9399999976158142/0.23326517641544342\n",
      "[validation]accuracy/loss:0.9428571462631226/0.22728389501571655\n",
      "[validation]accuracy/loss:0.9437500238418579/0.22072719037532806\n",
      "[validation]accuracy/loss:0.941111147403717/0.21853268146514893\n",
      "[validation]accuracy/loss:0.9459999799728394/0.20397384464740753\n",
      "[validation]accuracy/loss:0.9481818675994873/0.19468817114830017\n",
      "[validation]accuracy/loss:0.9474999904632568/0.19245681166648865\n",
      "[validation]accuracy/loss:0.944615364074707/0.19392642378807068\n",
      "[validation]accuracy/loss:0.9457142949104309/0.18748708069324493\n",
      "[validation]accuracy/loss:0.9466666579246521/0.1822720766067505\n",
      "[validation]accuracy/loss:0.9474999904632568/0.17660482227802277\n",
      "[validation]accuracy/loss:0.9476470947265625/0.17445026338100433\n",
      "[validation]accuracy/loss:0.9472222924232483/0.17541112005710602\n",
      "[validation]accuracy/loss:0.9489474296569824/0.17140468955039978\n",
      "[validation]accuracy/loss:0.9470000267028809/0.17694132030010223\n",
      "[validation]accuracy/loss:0.9438095092773438/0.18396757543087006\n",
      "[validation]accuracy/loss:0.9418181777000427/0.19113916158676147\n",
      "[validation]accuracy/loss:0.9413043260574341/0.19520366191864014\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[validation]accuracy/loss:0.9404166340827942/0.19856096804141998\n",
      "[validation]accuracy/loss:0.9408000111579895/0.19487084448337555\n",
      "[validation]accuracy/loss:0.9419230818748474/0.1901082843542099\n",
      "[validation]accuracy/loss:0.9414814710617065/0.1897754967212677\n",
      "[validation]accuracy/loss:0.9399999976158142/0.19287998974323273\n",
      "[validation]accuracy/loss:0.9372413754463196/0.19992758333683014\n",
      "[validation]accuracy/loss:0.9346666932106018/0.20781753957271576\n",
      "[validation]accuracy/loss:0.934516191482544/0.2090420424938202\n",
      "[validation]accuracy/loss:0.934999942779541/0.20748375356197357\n",
      "[validation]accuracy/loss:0.9357575178146362/0.2055860161781311\n",
      "[validation]accuracy/loss:0.9364705085754395/0.2025947868824005\n",
      "[validation]accuracy/loss:0.9374284744262695/0.20025087893009186\n",
      "[validation]accuracy/loss:0.9363888502120972/0.20295226573944092\n",
      "[validation]accuracy/loss:0.9362161755561829/0.20430178940296173\n",
      "[validation]accuracy/loss:0.9368420839309692/0.20270997285842896\n",
      "[validation]accuracy/loss:0.9369230270385742/0.20269092917442322\n",
      "[validation]accuracy/loss:0.9364999532699585/0.20357729494571686\n",
      "[validation]accuracy/loss:0.9353657960891724/0.20571105182170868\n",
      "[validation]accuracy/loss:0.9361904859542847/0.20325183868408203\n",
      "[validation]accuracy/loss:0.9367441534996033/0.20106644928455353\n",
      "[validation]accuracy/loss:0.9370454549789429/0.19977916777133942\n",
      "[validation]accuracy/loss:0.9375555515289307/0.19752000272274017\n",
      "[validation]accuracy/loss:0.9373912811279297/0.19914449751377106\n",
      "[validation]accuracy/loss:0.93808513879776/0.19683784246444702\n",
      "[validation]accuracy/loss:0.9385416507720947/0.19530156254768372\n",
      "[validation]accuracy/loss:0.9377551078796387/0.19770541787147522\n",
      "[validation]accuracy/loss:0.9368000030517578/0.20004963874816895\n",
      "[validation]accuracy/loss:0.9368627071380615/0.1994052529335022\n",
      "[validation]accuracy/loss:0.9371153712272644/0.19908924400806427\n",
      "[validation]accuracy/loss:0.9377358555793762/0.19747911393642426\n",
      "[validation]accuracy/loss:0.9381481409072876/0.19680948555469513\n",
      "[validation]accuracy/loss:0.9383636713027954/0.19580110907554626\n",
      "[validation]accuracy/loss:0.9391070604324341/0.1947355717420578\n",
      "[validation]accuracy/loss:0.9396491050720215/0.19323579967021942\n",
      "[validation]accuracy/loss:0.9393102526664734/0.19557678699493408\n",
      "[validation]accuracy/loss:0.9388135075569153/0.19537487626075745\n",
      "[validation]accuracy/loss:0.9391666054725647/0.19430264830589294\n",
      "[validation]accuracy/loss:0.9395080804824829/0.19334040582180023\n",
      "[validation]accuracy/loss:0.9401611685752869/0.19173631072044373\n",
      "[validation]accuracy/loss:0.9399998784065247/0.1927236020565033\n",
      "[validation]accuracy/loss:0.9404687285423279/0.19178302586078644\n",
      "[validation]accuracy/loss:0.9404615163803101/0.19158168137073517\n",
      "[validation]accuracy/loss:0.9409090876579285/0.19002829492092133\n",
      "[validation]accuracy/loss:0.9402984976768494/0.1901223063468933\n",
      "[validation]accuracy/loss:0.94102942943573/0.1881767362356186\n",
      "[validation]accuracy/loss:0.9410144686698914/0.18841864168643951\n",
      "[validation]accuracy/loss:0.9412857294082642/0.1870478391647339\n",
      "[validation]accuracy/loss:0.9415493011474609/0.18690750002861023\n",
      "[validation]accuracy/loss:0.9422221779823303/0.18514400720596313\n",
      "[validation]accuracy/loss:0.9426026940345764/0.18462839722633362\n",
      "[validation]accuracy/loss:0.9424324035644531/0.18480689823627472\n",
      "[validation]accuracy/loss:0.9425332546234131/0.1844467967748642\n",
      "[validation]accuracy/loss:0.9419736266136169/0.18528978526592255\n",
      "[validation]accuracy/loss:0.9419480562210083/0.18585865199565887\n",
      "[validation]accuracy/loss:0.9414102435112/0.18672850728034973\n",
      "[validation]accuracy/loss:0.9415189623832703/0.18598301708698273\n",
      "[validation]accuracy/loss:0.9418748617172241/0.18512120842933655\n",
      "[validation]accuracy/loss:0.9423456192016602/0.18430763483047485\n",
      "[validation]accuracy/loss:0.9429267048835754/0.18307794630527496\n",
      "[validation]accuracy/loss:0.9434938430786133/0.18146860599517822\n",
      "[validation]accuracy/loss:0.9439284801483154/0.1800825446844101\n",
      "[validation]accuracy/loss:0.944352924823761/0.17886972427368164\n",
      "[validation]accuracy/loss:0.9448836445808411/0.17746374011039734\n",
      "[validation]accuracy/loss:0.9451723694801331/0.17642714083194733\n",
      "[validation]accuracy/loss:0.9455680847167969/0.17535589635372162\n",
      "[validation]accuracy/loss:0.9452807903289795/0.17523926496505737\n",
      "[validation]accuracy/loss:0.9456666111946106/0.17376643419265747\n",
      "[validation]accuracy/loss:0.9462636709213257/0.172160804271698\n",
      "[validation]accuracy/loss:0.9467390179634094/0.17096787691116333\n",
      "[validation]accuracy/loss:0.9469891786575317/0.1702696830034256\n",
      "[validation]accuracy/loss:0.9469148516654968/0.17077331244945526\n",
      "[validation]accuracy/loss:0.9468420743942261/0.17077647149562836\n",
      "[validation]accuracy/loss:0.9473958015441895/0.16926075518131256\n",
      "[validation]accuracy/loss:0.9477319717407227/0.1683209240436554\n",
      "[validation]accuracy/loss:0.9471428394317627/0.17248313128948212\n",
      "[validation]accuracy/loss:0.9474747776985168/0.17136967182159424\n",
      "[validation]accuracy/loss:0.9474000334739685/0.17220962047576904\n",
      "epoch:2,batch:0,loss is:[0.12425289]\n",
      "epoch:2,batch:200,loss is:[0.16008852]\n",
      "epoch:2,batch:400,loss is:[0.22456856]\n",
      "[validation]accuracy/loss:0.9800000190734863/0.10695549100637436\n",
      "[validation]accuracy/loss:0.9650000333786011/0.12416968494653702\n",
      "[validation]accuracy/loss:0.9566667079925537/0.15568773448467255\n",
      "[validation]accuracy/loss:0.940000057220459/0.19461604952812195\n",
      "[validation]accuracy/loss:0.9360000491142273/0.19618439674377441\n",
      "[validation]accuracy/loss:0.93666672706604/0.19702070951461792\n",
      "[validation]accuracy/loss:0.9371429085731506/0.18796394765377045\n",
      "[validation]accuracy/loss:0.9399999976158142/0.1786280870437622\n",
      "[validation]accuracy/loss:0.9399999976158142/0.17279121279716492\n",
      "[validation]accuracy/loss:0.9440000653266907/0.16285444796085358\n",
      "[validation]accuracy/loss:0.9463636875152588/0.1536068618297577\n",
      "[validation]accuracy/loss:0.9475000500679016/0.14972874522209167\n",
      "[validation]accuracy/loss:0.9461539387702942/0.15096315741539001\n",
      "[validation]accuracy/loss:0.9478572607040405/0.14599770307540894\n",
      "[validation]accuracy/loss:0.9493334293365479/0.141531839966774\n",
      "[validation]accuracy/loss:0.9506250619888306/0.13605768978595734\n",
      "[validation]accuracy/loss:0.9511764645576477/0.1339167207479477\n",
      "[validation]accuracy/loss:0.9522221684455872/0.13436226546764374\n",
      "[validation]accuracy/loss:0.9542104601860046/0.13061080873012543\n",
      "[validation]accuracy/loss:0.9524999856948853/0.13500620424747467\n",
      "[validation]accuracy/loss:0.9509523510932922/0.13860337436199188\n",
      "[validation]accuracy/loss:0.9490908980369568/0.14318393170833588\n",
      "[validation]accuracy/loss:0.9478260278701782/0.14747881889343262\n",
      "[validation]accuracy/loss:0.9479166865348816/0.14686256647109985\n",
      "[validation]accuracy/loss:0.9487999677658081/0.14456740021705627\n",
      "[validation]accuracy/loss:0.9507691860198975/0.14009878039360046\n",
      "[validation]accuracy/loss:0.9514814019203186/0.13905036449432373\n",
      "[validation]accuracy/loss:0.9510713815689087/0.14189869165420532\n",
      "[validation]accuracy/loss:0.949999988079071/0.14739641547203064\n",
      "[validation]accuracy/loss:0.9486666321754456/0.15325430035591125\n",
      "[validation]accuracy/loss:0.948387086391449/0.15319283306598663\n",
      "[validation]accuracy/loss:0.948437511920929/0.15192030370235443\n",
      "[validation]accuracy/loss:0.9493939280509949/0.150592640042305\n",
      "[validation]accuracy/loss:0.949999988079071/0.1487078219652176\n",
      "[validation]accuracy/loss:0.9508571028709412/0.1472296267747879\n",
      "[validation]accuracy/loss:0.9505555033683777/0.14876540005207062\n",
      "[validation]accuracy/loss:0.9497296214103699/0.1515684425830841\n",
      "[validation]accuracy/loss:0.9502630829811096/0.151237353682518\n",
      "[validation]accuracy/loss:0.95025634765625/0.1515960544347763\n",
      "[validation]accuracy/loss:0.9497500658035278/0.15225595235824585\n",
      "[validation]accuracy/loss:0.9487805366516113/0.153703972697258\n",
      "[validation]accuracy/loss:0.9497619867324829/0.15132728219032288\n",
      "[validation]accuracy/loss:0.9502326846122742/0.15022239089012146\n",
      "[validation]accuracy/loss:0.9506819248199463/0.14912627637386322\n",
      "[validation]accuracy/loss:0.9515557289123535/0.14706209301948547\n",
      "[validation]accuracy/loss:0.9515219330787659/0.14855924248695374\n",
      "[validation]accuracy/loss:0.9521278142929077/0.14712752401828766\n",
      "[validation]accuracy/loss:0.95250004529953/0.14642702043056488\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[validation]accuracy/loss:0.9518367052078247/0.14882710576057434\n",
      "[validation]accuracy/loss:0.9509999752044678/0.15006530284881592\n",
      "[validation]accuracy/loss:0.9511764645576477/0.14903852343559265\n",
      "[validation]accuracy/loss:0.9511538147926331/0.1487976461648941\n",
      "[validation]accuracy/loss:0.951509416103363/0.14797312021255493\n",
      "[validation]accuracy/loss:0.9520370364189148/0.14771310985088348\n",
      "[validation]accuracy/loss:0.9521818161010742/0.1467667520046234\n",
      "[validation]accuracy/loss:0.9526785612106323/0.1458999365568161\n",
      "[validation]accuracy/loss:0.9529824256896973/0.1450187861919403\n",
      "[validation]accuracy/loss:0.9529310464859009/0.14699910581111908\n",
      "[validation]accuracy/loss:0.952881395816803/0.14683230221271515\n",
      "[validation]accuracy/loss:0.9535000324249268/0.1456373929977417\n",
      "[validation]accuracy/loss:0.9536066055297852/0.14492906630039215\n",
      "[validation]accuracy/loss:0.9541935920715332/0.14357338845729828\n",
      "[validation]accuracy/loss:0.9541270732879639/0.1441471427679062\n",
      "[validation]accuracy/loss:0.9542187452316284/0.14350256323814392\n",
      "[validation]accuracy/loss:0.9543076753616333/0.14397189021110535\n",
      "[validation]accuracy/loss:0.9548484683036804/0.14260002970695496\n",
      "[validation]accuracy/loss:0.9546268582344055/0.14301355183124542\n",
      "[validation]accuracy/loss:0.9550000429153442/0.14180004596710205\n",
      "[validation]accuracy/loss:0.9550724625587463/0.14211536943912506\n",
      "[validation]accuracy/loss:0.9555714130401611/0.14097939431667328\n",
      "[validation]accuracy/loss:0.9557746648788452/0.14138874411582947\n",
      "[validation]accuracy/loss:0.9563888907432556/0.1398804932832718\n",
      "[validation]accuracy/loss:0.9565753936767578/0.13928592205047607\n",
      "[validation]accuracy/loss:0.957027018070221/0.1384495347738266\n",
      "[validation]accuracy/loss:0.9571999907493591/0.13812659680843353\n",
      "[validation]accuracy/loss:0.9569737315177917/0.13854466378688812\n",
      "[validation]accuracy/loss:0.9568831324577332/0.1388532966375351\n",
      "[validation]accuracy/loss:0.9565384984016418/0.13963454961776733\n",
      "[validation]accuracy/loss:0.9567089080810547/0.1390744149684906\n",
      "[validation]accuracy/loss:0.9569999575614929/0.13821053504943848\n",
      "[validation]accuracy/loss:0.9572839736938477/0.13757489621639252\n",
      "[validation]accuracy/loss:0.9575610160827637/0.13678066432476044\n",
      "[validation]accuracy/loss:0.9579518437385559/0.13538716733455658\n",
      "[validation]accuracy/loss:0.9584524035453796/0.13418862223625183\n",
      "[validation]accuracy/loss:0.9588235020637512/0.13298915326595306\n",
      "[validation]accuracy/loss:0.9593023061752319/0.1318386048078537\n",
      "[validation]accuracy/loss:0.9595402479171753/0.13097883760929108\n",
      "[validation]accuracy/loss:0.959886372089386/0.13003185391426086\n",
      "[validation]accuracy/loss:0.9596629738807678/0.13051316142082214\n",
      "[validation]accuracy/loss:0.960111141204834/0.12930364906787872\n",
      "[validation]accuracy/loss:0.9605494737625122/0.12804022431373596\n",
      "[validation]accuracy/loss:0.9608696103096008/0.1269305944442749\n",
      "[validation]accuracy/loss:0.9610753059387207/0.12647098302841187\n",
      "[validation]accuracy/loss:0.9608511328697205/0.1275174468755722\n",
      "[validation]accuracy/loss:0.9608421921730042/0.12737558782100677\n",
      "[validation]accuracy/loss:0.9612500071525574/0.12614697217941284\n",
      "[validation]accuracy/loss:0.961443305015564/0.12556667625904083\n",
      "[validation]accuracy/loss:0.9610204100608826/0.1290748119354248\n",
      "[validation]accuracy/loss:0.961212158203125/0.12812809646129608\n",
      "[validation]accuracy/loss:0.9613000750541687/0.12937557697296143\n",
      "epoch:3,batch:0,loss is:[0.13515794]\n",
      "epoch:3,batch:200,loss is:[0.20801271]\n",
      "epoch:3,batch:400,loss is:[0.10828901]\n",
      "[validation]accuracy/loss:0.9700000286102295/0.09782299399375916\n",
      "[validation]accuracy/loss:0.9700000286102295/0.11215844750404358\n",
      "[validation]accuracy/loss:0.9666666984558105/0.13649651408195496\n",
      "[validation]accuracy/loss:0.95250004529953/0.17171348631381989\n",
      "[validation]accuracy/loss:0.9480000734329224/0.17005527019500732\n",
      "[validation]accuracy/loss:0.9516666531562805/0.1712859570980072\n",
      "[validation]accuracy/loss:0.9528571367263794/0.1663440614938736\n",
      "[validation]accuracy/loss:0.9524999856948853/0.15940521657466888\n",
      "[validation]accuracy/loss:0.9500000476837158/0.15522703528404236\n",
      "[validation]accuracy/loss:0.9530000686645508/0.14584143459796906\n",
      "[validation]accuracy/loss:0.9545455574989319/0.1382601410150528\n",
      "[validation]accuracy/loss:0.9566667675971985/0.1352396458387375\n",
      "[validation]accuracy/loss:0.9553846716880798/0.1360829472541809\n",
      "[validation]accuracy/loss:0.9564286470413208/0.13186079263687134\n",
      "[validation]accuracy/loss:0.9580000638961792/0.1275239884853363\n",
      "[validation]accuracy/loss:0.9581249952316284/0.1229129210114479\n",
      "[validation]accuracy/loss:0.9588235020637512/0.12024345248937607\n",
      "[validation]accuracy/loss:0.9588887691497803/0.12266181409358978\n",
      "[validation]accuracy/loss:0.9599998593330383/0.11937227100133896\n",
      "[validation]accuracy/loss:0.9584999084472656/0.12488215416669846\n",
      "[validation]accuracy/loss:0.9585713148117065/0.12711654603481293\n",
      "[validation]accuracy/loss:0.9581817388534546/0.12860634922981262\n",
      "[validation]accuracy/loss:0.9573912620544434/0.13210491836071014\n",
      "[validation]accuracy/loss:0.9574999809265137/0.12994284927845\n",
      "[validation]accuracy/loss:0.9579999446868896/0.12774761021137238\n",
      "[validation]accuracy/loss:0.9596153497695923/0.12345778197050095\n",
      "[validation]accuracy/loss:0.9599999189376831/0.1224953681230545\n",
      "[validation]accuracy/loss:0.9582142233848572/0.12680445611476898\n",
      "[validation]accuracy/loss:0.9568964838981628/0.1320614069700241\n",
      "[validation]accuracy/loss:0.9563332796096802/0.13881713151931763\n",
      "[validation]accuracy/loss:0.9570966958999634/0.13832056522369385\n",
      "[validation]accuracy/loss:0.9578125476837158/0.13714993000030518\n",
      "[validation]accuracy/loss:0.9587879180908203/0.13573558628559113\n",
      "[validation]accuracy/loss:0.9591176509857178/0.1339336633682251\n",
      "[validation]accuracy/loss:0.9597142934799194/0.13234764337539673\n",
      "[validation]accuracy/loss:0.9591666460037231/0.13327041268348694\n",
      "[validation]accuracy/loss:0.9589189291000366/0.13590730726718903\n",
      "[validation]accuracy/loss:0.9592105746269226/0.1349485218524933\n",
      "[validation]accuracy/loss:0.958717942237854/0.13592037558555603\n",
      "[validation]accuracy/loss:0.9589999914169312/0.13589774072170258\n",
      "[validation]accuracy/loss:0.9587805271148682/0.1371658891439438\n",
      "[validation]accuracy/loss:0.9595238566398621/0.13506203889846802\n",
      "[validation]accuracy/loss:0.9600000381469727/0.13383793830871582\n",
      "[validation]accuracy/loss:0.9604545831680298/0.1326281875371933\n",
      "[validation]accuracy/loss:0.9611111879348755/0.1307620108127594\n",
      "[validation]accuracy/loss:0.9608696699142456/0.1324562430381775\n",
      "[validation]accuracy/loss:0.9612767100334167/0.13096491992473602\n",
      "[validation]accuracy/loss:0.9616666436195374/0.12948481738567352\n",
      "[validation]accuracy/loss:0.9608162641525269/0.1313200294971466\n",
      "[validation]accuracy/loss:0.9603999257087708/0.13275554776191711\n",
      "[validation]accuracy/loss:0.9605882167816162/0.13196226954460144\n",
      "[validation]accuracy/loss:0.9605768918991089/0.13202807307243347\n",
      "[validation]accuracy/loss:0.960943341255188/0.13144825398921967\n",
      "[validation]accuracy/loss:0.9612962007522583/0.13120229542255402\n",
      "[validation]accuracy/loss:0.9614545106887817/0.13060221076011658\n",
      "[validation]accuracy/loss:0.9617857336997986/0.13021908700466156\n",
      "[validation]accuracy/loss:0.9621052742004395/0.12939858436584473\n",
      "[validation]accuracy/loss:0.961896538734436/0.13147513568401337\n",
      "[validation]accuracy/loss:0.9618644118309021/0.13215884566307068\n",
      "[validation]accuracy/loss:0.9623333811759949/0.1307407021522522\n",
      "[validation]accuracy/loss:0.9622951149940491/0.1302182525396347\n",
      "[validation]accuracy/loss:0.9627419710159302/0.12896728515625\n",
      "[validation]accuracy/loss:0.9626984596252441/0.12971876561641693\n",
      "[validation]accuracy/loss:0.9629687666893005/0.12900330126285553\n",
      "[validation]accuracy/loss:0.9630769491195679/0.12874054908752441\n",
      "[validation]accuracy/loss:0.9633333683013916/0.1276187002658844\n",
      "[validation]accuracy/loss:0.9626865386962891/0.12788033485412598\n",
      "[validation]accuracy/loss:0.9630882143974304/0.12658992409706116\n",
      "[validation]accuracy/loss:0.9628984928131104/0.12676019966602325\n",
      "[validation]accuracy/loss:0.9629999399185181/0.12592466175556183\n",
      "[validation]accuracy/loss:0.9628167748451233/0.12635476887226105\n",
      "[validation]accuracy/loss:0.9631944298744202/0.12499503046274185\n",
      "[validation]accuracy/loss:0.9632876515388489/0.1247839629650116\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[validation]accuracy/loss:0.9636486172676086/0.1242058277130127\n",
      "[validation]accuracy/loss:0.963866651058197/0.12384942919015884\n",
      "[validation]accuracy/loss:0.9638158082962036/0.12404476851224899\n",
      "[validation]accuracy/loss:0.9638960957527161/0.12412859499454498\n",
      "[validation]accuracy/loss:0.9633333086967468/0.12489710748195648\n",
      "[validation]accuracy/loss:0.9634177088737488/0.12430296093225479\n",
      "[validation]accuracy/loss:0.9637500047683716/0.12347526848316193\n",
      "[validation]accuracy/loss:0.9639506340026855/0.12291403114795685\n",
      "[validation]accuracy/loss:0.9642682671546936/0.1220620647072792\n",
      "[validation]accuracy/loss:0.9645782709121704/0.12079887092113495\n",
      "[validation]accuracy/loss:0.9649999737739563/0.11965587735176086\n",
      "[validation]accuracy/loss:0.9652940630912781/0.11864828318357468\n",
      "[validation]accuracy/loss:0.9655812978744507/0.11767994612455368\n",
      "[validation]accuracy/loss:0.965747058391571/0.11682379990816116\n",
      "[validation]accuracy/loss:0.9661363363265991/0.11589492857456207\n",
      "[validation]accuracy/loss:0.9659549593925476/0.11617272347211838\n",
      "[validation]accuracy/loss:0.9662221074104309/0.11509590595960617\n",
      "[validation]accuracy/loss:0.966593325138092/0.11394032090902328\n",
      "[validation]accuracy/loss:0.9669564366340637/0.11288274079561234\n",
      "[validation]accuracy/loss:0.967096745967865/0.11252609640359879\n",
      "[validation]accuracy/loss:0.9668084979057312/0.11379223316907883\n",
      "[validation]accuracy/loss:0.9667367935180664/0.11378747224807739\n",
      "[validation]accuracy/loss:0.9670832753181458/0.11269915103912354\n",
      "[validation]accuracy/loss:0.9671133756637573/0.11237536370754242\n",
      "[validation]accuracy/loss:0.9667346477508545/0.11615987867116928\n",
      "[validation]accuracy/loss:0.9668686985969543/0.11539600044488907\n",
      "[validation]accuracy/loss:0.9667999744415283/0.1166837066411972\n",
      "epoch:4,batch:0,loss is:[0.13920374]\n",
      "epoch:4,batch:200,loss is:[0.09957095]\n",
      "epoch:4,batch:400,loss is:[0.08906779]\n",
      "[validation]accuracy/loss:0.9900000095367432/0.0499279610812664\n",
      "[validation]accuracy/loss:0.9800000190734863/0.07591398060321808\n",
      "[validation]accuracy/loss:0.9733333587646484/0.09657123684883118\n",
      "[validation]accuracy/loss:0.9625000357627869/0.127321258187294\n",
      "[validation]accuracy/loss:0.9620000123977661/0.12495570629835129\n",
      "[validation]accuracy/loss:0.9616666436195374/0.1267988532781601\n",
      "[validation]accuracy/loss:0.9642857313156128/0.12269260734319687\n",
      "[validation]accuracy/loss:0.9624999761581421/0.11972051858901978\n",
      "[validation]accuracy/loss:0.9599999189376831/0.11892551183700562\n",
      "[validation]accuracy/loss:0.9619998931884766/0.11197800934314728\n",
      "[validation]accuracy/loss:0.9636362195014954/0.10566013306379318\n",
      "[validation]accuracy/loss:0.9658331871032715/0.10181045532226562\n",
      "[validation]accuracy/loss:0.9646152257919312/0.10363265126943588\n",
      "[validation]accuracy/loss:0.9664283990859985/0.1000162810087204\n",
      "[validation]accuracy/loss:0.9679998159408569/0.09644879400730133\n",
      "[validation]accuracy/loss:0.96875/0.09282936155796051\n",
      "[validation]accuracy/loss:0.9682352542877197/0.09200554341077805\n",
      "[validation]accuracy/loss:0.9683332443237305/0.09448021650314331\n",
      "[validation]accuracy/loss:0.9694736003875732/0.09153451025485992\n",
      "[validation]accuracy/loss:0.9679999351501465/0.09550222009420395\n",
      "[validation]accuracy/loss:0.9685713648796082/0.09702415019273758\n",
      "[validation]accuracy/loss:0.9677272439002991/0.10006969422101974\n",
      "[validation]accuracy/loss:0.9665217399597168/0.10551930218935013\n",
      "[validation]accuracy/loss:0.9670833945274353/0.10378996282815933\n",
      "[validation]accuracy/loss:0.9672000408172607/0.10277353227138519\n",
      "[validation]accuracy/loss:0.9684615731239319/0.09939801692962646\n",
      "[validation]accuracy/loss:0.9688888788223267/0.0980171486735344\n",
      "[validation]accuracy/loss:0.9682143330574036/0.09886839985847473\n",
      "[validation]accuracy/loss:0.9668965935707092/0.10381773114204407\n",
      "[validation]accuracy/loss:0.9656667113304138/0.11037848889827728\n",
      "[validation]accuracy/loss:0.9658064842224121/0.11051403731107712\n",
      "[validation]accuracy/loss:0.9662500023841858/0.10924390703439713\n",
      "[validation]accuracy/loss:0.9669696688652039/0.10825711488723755\n",
      "[validation]accuracy/loss:0.9670588374137878/0.10713690519332886\n",
      "[validation]accuracy/loss:0.9674285650253296/0.10608652234077454\n",
      "[validation]accuracy/loss:0.9672222137451172/0.10767482966184616\n",
      "[validation]accuracy/loss:0.9667567610740662/0.11075229942798615\n",
      "[validation]accuracy/loss:0.9668421745300293/0.10996562987565994\n",
      "[validation]accuracy/loss:0.9664103388786316/0.11051566898822784\n",
      "[validation]accuracy/loss:0.9665001034736633/0.11083106696605682\n",
      "[validation]accuracy/loss:0.9665854573249817/0.11096331477165222\n",
      "[validation]accuracy/loss:0.9673810601234436/0.10895808041095734\n",
      "[validation]accuracy/loss:0.9676744937896729/0.10826343297958374\n",
      "[validation]accuracy/loss:0.9679546356201172/0.10700933635234833\n",
      "[validation]accuracy/loss:0.9684445858001709/0.10569781810045242\n",
      "[validation]accuracy/loss:0.9680436253547668/0.10718084871768951\n",
      "[validation]accuracy/loss:0.9682980179786682/0.10604487359523773\n",
      "[validation]accuracy/loss:0.9683334231376648/0.10549681633710861\n",
      "[validation]accuracy/loss:0.9675511121749878/0.10785627365112305\n",
      "[validation]accuracy/loss:0.967400074005127/0.10874177515506744\n",
      "[validation]accuracy/loss:0.9676470756530762/0.10798323154449463\n",
      "[validation]accuracy/loss:0.9676923751831055/0.10746908187866211\n",
      "[validation]accuracy/loss:0.9679245948791504/0.10686970502138138\n",
      "[validation]accuracy/loss:0.9681481719017029/0.10691050440073013\n",
      "[validation]accuracy/loss:0.9681819081306458/0.10628160834312439\n",
      "[validation]accuracy/loss:0.9683929085731506/0.1059887707233429\n",
      "[validation]accuracy/loss:0.9685965180397034/0.10511748492717743\n",
      "[validation]accuracy/loss:0.968448281288147/0.10702180862426758\n",
      "[validation]accuracy/loss:0.9683051109313965/0.10719384998083115\n",
      "[validation]accuracy/loss:0.9686667323112488/0.10607422143220901\n",
      "[validation]accuracy/loss:0.9685246348381042/0.10573999583721161\n",
      "[validation]accuracy/loss:0.9690322875976562/0.10450922697782516\n",
      "[validation]accuracy/loss:0.9685714244842529/0.10558785498142242\n",
      "[validation]accuracy/loss:0.9685937762260437/0.10507051646709442\n",
      "[validation]accuracy/loss:0.9686154127120972/0.1048879399895668\n",
      "[validation]accuracy/loss:0.9687879085540771/0.10416542738676071\n",
      "[validation]accuracy/loss:0.9685074687004089/0.10444186627864838\n",
      "[validation]accuracy/loss:0.9686765074729919/0.10355746746063232\n",
      "[validation]accuracy/loss:0.9684057831764221/0.10399865359067917\n",
      "[validation]accuracy/loss:0.9687142372131348/0.10334504395723343\n",
      "[validation]accuracy/loss:0.9687323570251465/0.10389970242977142\n",
      "[validation]accuracy/loss:0.9690278172492981/0.10281942039728165\n",
      "[validation]accuracy/loss:0.9689041376113892/0.10275138914585114\n",
      "[validation]accuracy/loss:0.9691892266273499/0.10239847004413605\n",
      "[validation]accuracy/loss:0.9693334102630615/0.10217396914958954\n",
      "[validation]accuracy/loss:0.9693421721458435/0.1018204540014267\n",
      "[validation]accuracy/loss:0.9692208170890808/0.10225977748632431\n",
      "[validation]accuracy/loss:0.9691026210784912/0.10246192663908005\n",
      "[validation]accuracy/loss:0.9691140055656433/0.10235895216464996\n",
      "[validation]accuracy/loss:0.9692500829696655/0.10178254544734955\n",
      "[validation]accuracy/loss:0.9693828821182251/0.10128167271614075\n",
      "[validation]accuracy/loss:0.9696342945098877/0.10055694729089737\n",
      "[validation]accuracy/loss:0.9700000882148743/0.09945090115070343\n",
      "[validation]accuracy/loss:0.9703572392463684/0.09853659570217133\n",
      "[validation]accuracy/loss:0.9705883264541626/0.09777706116437912\n",
      "[validation]accuracy/loss:0.9709303379058838/0.09687652438879013\n",
      "[validation]accuracy/loss:0.971034586429596/0.09627358615398407\n",
      "[validation]accuracy/loss:0.9713636636734009/0.09542729705572128\n",
      "[validation]accuracy/loss:0.9708989262580872/0.09607409685850143\n",
      "[validation]accuracy/loss:0.9712222814559937/0.09516890347003937\n",
      "[validation]accuracy/loss:0.9715384840965271/0.09418834745883942\n",
      "[validation]accuracy/loss:0.9717391729354858/0.09336327761411667\n",
      "[validation]accuracy/loss:0.9717204570770264/0.09320404380559921\n",
      "[validation]accuracy/loss:0.9713830351829529/0.09410940855741501\n",
      "[validation]accuracy/loss:0.9713684916496277/0.09412015229463577\n",
      "[validation]accuracy/loss:0.9716667532920837/0.09321101754903793\n",
      "[validation]accuracy/loss:0.9717527031898499/0.09315374493598938\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[validation]accuracy/loss:0.9713266491889954/0.09655290842056274\n",
      "[validation]accuracy/loss:0.9714142680168152/0.0959758460521698\n",
      "[validation]accuracy/loss:0.9714001417160034/0.09740221500396729\n"
     ]
    }
   ],
   "source": [
    "train_loader=load_data('train')\n",
    "valid_loader=load_data('valid')\n",
    "model=LeNetModel()\n",
    "train(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "paddle.save(model.state_dict(),'mnist-cnn.pdparams')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "im=Image.open('0.jpg').convert('L')\n",
    "im=im.resize((28,28),Image.ANTIALIAS)\n",
    "img=np.array(im).reshape(1,1,28,28).astype('float32')\n",
    "img=1.0-img/255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 200x200 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.figure(figsize=(2,2))\n",
    "plt.imshow(im,cmap=plt.cm.binary)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本次预测的数字是: 8\n"
     ]
    }
   ],
   "source": [
    "model=LeNetModel()\n",
    "params_file_path='mnist-cnn.pdparams'\n",
    "param_dict=paddle.load(params_file_path)\n",
    "model.load_dict(param_dict)\n",
    "model.eval()\n",
    "tensor_img=img\n",
    "results=model(paddle.to_tensor(tensor_img))\n",
    "lab=np.argsort(results.numpy())\n",
    "print(\"本次预测的数字是:\",lab[0][-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
